package com.kira.scaffoldmvc.Test;

import cn.hutool.json.JSONUtil;
import com.kira.scaffoldmvc.POJO.LoginOrRegistry;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;

class LoginRequest {
    private String email;
    private String otp;

    public LoginRequest(String email, String otp) {
        this.email = email;
        this.otp = otp;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getOtp() {
        return otp;
    }

    public void setOtp(String otp) {
        this.otp = otp;
    }
}

public class LoginHttpClientExample {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 创建LoginRequest对象并设置属性
        LoginRequest requestData = new LoginRequest("1657294032@qq.com", "773728");
        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(requestData);

        // 创建HttpRequest实例，设置请求URL、请求方式和请求体
        HttpRequest request = HttpRequest.newBuilder()
               .uri(URI.create("http://47.119.148.251:7000/user/login"))
               .header("Content-Type", "application/json")
               .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
               .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
        LoginOrRegistry loginResponse = JSONUtil.toBean(response.body(), LoginOrRegistry.class);
        // 处理响应
        System.out.println("响应状态码: " + response.statusCode());
        System.out.println("响应体内容: " + response.body());
        System.out.println("响应体内容: " + response.headers());


    }
}